package mysql

import (
	"furni-set/funrpc/funrpc"

	"gorm.io/gorm"
)

type Fruit struct {
	gorm.Model
	FruitName  string  `gorm:"type:varchar(30);not null;commit:'水果商品名称'"`
	FruitPrice float64 `gorm:"type:decimal(10,2);not null;commit:'水果价格'"`
	FruitNum   int64   `gorm:"type:int(11);not null;commit:'水果数量'"`
	BrandId    int64   `gorm:"type:int(11);not null;commit:'品牌id'"`
	SortId     int64   `gorm:"type:int(11);not null;commit:'分类id'"`
}

func (f *Fruit) FruitAdd(db *gorm.DB) error {
	return db.Debug().Create(f).Error
}

func (f *Fruit) FindFruit(db *gorm.DB, name string) error {
	return db.Debug().Where("fruit_name=?", name).First(f).Error
}

func (f *Fruit) ShowFruit(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Find(f).Error
}

func List(db *gorm.DB, lists []*funrpc.FruitList) []*funrpc.FruitList {
	db.Debug().Model(&Fruit{}).Select("fruits.id as fruits_id", "fruit_name",
		"fruit_price", "fruit_num", "brands.brand_name as brand_name", "sorts.sort_name as sort_name").
		Joins("JOIN brands ON fruits.brand_id =brands.id").Joins("JOIN sorts ON fruits.sort_id =sorts.id").Find(&lists)
	return lists
}
